﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameFramework.GameLogic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using GameFramework.Controller;

namespace MyRaceGame.GameLogic.GameStates
{
    public class FinishState : RaceGameState
    {
        public FinishState(RaceGame raceGame)
            : base(raceGame)
        {
        }

        protected override void UpdateKeyboard(GameTime gameTime)
        {
            if (InputController.KeyPressed(Keys.Space))
            {
                raceGame.Reset();
                reset = true;
            }

            if (raceGame.CameraMode == CameraMode.ChaseCamera && InputController.KeyPressed(Keys.Q))
                raceGame.ChangeTrackedRacer();

            if (InputController.KeyPressed(Keys.C))
                raceGame.ChangeCameraMode(CameraMode.ChaseCamera);

            if (InputController.KeyPressed(Keys.F))
                raceGame.ChangeCameraMode(CameraMode.FreeCamera);
        }

        protected override void UpdateTransition(GameTime gameTime)
        {
            if (reset)
                GameState = new BeginningState(raceGame);
        }

        protected override void UpdateRacers(GameTime gameTime)
        {
            RaceController.UpdateRacers(gameTime);
        }

        protected override void DrawRacers(GameTime gameTime)
        {
            raceGame.DrawModels();
        }

        protected override void DrawingAdditional(GameTime gameTime)
        {
            raceGame.DrawRaceTrack();
        }

        protected override void DrawText(GameTime gameTime)
        {
            TextDrawController.Begin(TextAlignment.Center);

            if (RaceController.FinishRacers.Count == 0)
                return;

            if (RaceController.Winner == RaceController.HumanRacer)
                TextDrawController.WriteLine("You win!");
            else
                TextDrawController.WriteLine("You lost!");

            TextDrawController.WriteLine("All standings:");

            int i = 1;

            foreach (TimeCountedRacer tcr in RaceController.FinishRacers)
                TextDrawController.WriteLine("{0}. Place : {1} ({2:mm}:{2:ss}.{2:fff})", i++, tcr.Name,
                    tcr.TotalTime);

            TextDrawController.WriteLine();
            TextDrawController.WriteLine("Press Escape to Exit");
            TextDrawController.WriteLine("Press Space to Restart");

            TextDrawController.End();
        }

        bool reset = false;
    }
}
